package com.lancoo.ccas53.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas53.entity.*;
import com.lancoo.ccas53.pojo.common.PageInfo;
import com.lancoo.ccas53.pojo.dto.*;
import com.lancoo.ccas53.pojo.vo.CssTaskInput;
import com.lancoo.ccas53.pojo.vo.StuCourseInput;
import com.lancoo.ccas53.service.StudentService;
import com.lancoo.ccas53.service.TeachingClassService;
import com.lancoo.ccas53.service.TeachingClassStudentService;
import com.lancoo.ccas53.service.impl.StudentServiceImpl;
import com.lancoo.ccas53.util.RedisUtil;
import com.lancoo.ccas53.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author quin
 * @Date 2024/5/21 19:03
 */
@Api(tags = "学生选课")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private TeachingClassStudentService teachingClassStudentService;
    @Resource
    private StudentServiceImpl studentServiceimpl;
    @Resource
    private TeachingClassService teachingClassService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("获取全部学生")
    @GetMapping(value = "/getStudentList")
    public ResponseObject<PageInfo<Student>> getStudentByConditions(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                    @ApiParam(value = "学院id ") @RequestParam(required = false) String baseCollegeId,
                                                                    @ApiParam(value = "专业id ") @RequestParam(required = false) String majorId,
                                                                    @ApiParam(value = "年级id ") @RequestParam(required = false) String gradeId,
                                                                    @ApiParam(value = "班级id ") @RequestParam(required = false) String classId,
                                                                    @ApiParam(value = "名称、学号模糊查询") @RequestParam(required = false) String keyword,
                                                                    @ApiParam(value = "选课范围 1本专业年级  2本专业跨年级 3本院系跨年级 4全部专业年级 ") @RequestParam(required = false) Integer selectionScope,
                                                                    @ApiParam(value = "选课范围（预选）") @RequestParam(required = false) Integer selectionScopePre,
                                                                    @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                    @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<Student> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(studentService.getStudentByConditions(taskId, page, baseCollegeId, majorId, gradeId, classId, keyword, selectionScope, selectionScopePre));
    }

    @ApiOperation("根据基础平台学生Id获取学生信息")
    @GetMapping(value = "/getStudentInfoByBaseStudentId")
    public ResponseObject<Student> getStudentInfoByBaseStudentId(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "基础平台学生id ", required = true) @RequestParam String baseStudentId) {
        return ResponseObject.ofSuccess(studentService.getStudentInfoByBaseStudentId(baseStudentId, taskId));
    }

    @ApiOperation("获取选课任务")
    @PostMapping(value = "/getCssTask")
    public ResponseObject<List<CssTaskDto>> getCssTask(@ApiParam(value = "参数") @RequestBody(required = false) CssTaskInput param) {
        return ResponseObject.ofSuccess(studentService.getCssTask(param));
    }

    @ApiOperation("获取选课课程列表")
    @PostMapping(value = "/getStudentCourseView")
    public ResponseObject<StuCourseDto> getStudentCourseView(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                             @ApiParam(value = "参数") @RequestBody(required = false) StuCourseInput param) {
        return ResponseObject.ofSuccess(studentService.getStudentCourseView(taskId, param));
    }

    @ApiOperation("获取预选课课程列表")
    @PostMapping(value = "/getPreCourseList")
    public ResponseObject<List<Course>> getPreCourseList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "轮次id") @RequestParam(required = false) Long roundId,
                                                         @ApiParam(value = "学生id") @RequestParam(required = false) Long studentId) {
        return ResponseObject.ofSuccess(studentService.getPreCourseList(taskId, roundId, studentId));
    }

    @ApiOperation("代学生选课")
    @PostMapping(value = "/teacherSelectToStu")
    public ResponseObject<Object> teacherSelectToStu(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "参数") @RequestBody(required = false) TeachingClassStudent param) {
        return ResponseObject.ofSuccess(studentService.teacherSelectToStu(taskId,param));
    }

    @ApiOperation("代学生退课")
    @PostMapping(value = "/teacherCancelToStu")
    public ResponseObject<Object> teacherCancelToStu(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "参数") @RequestBody(required = false) TeachingClassStudent param) {

        return ResponseObject.ofSuccess(studentService.teacherCancelToStu(taskId,param));
    }

    @ApiOperation("学生选教学班")
    @PostMapping(value = "/selectTeachingClass")
    public ResponseObject<Object> selectCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                               @ApiParam(value = "参数") @RequestBody(required = false) SelectCourseInput param) {
        Integer flag = studentService.selectCourse(taskId, param);
        switch (flag) {
            case 1:
                return ResponseObject.ofSuccess("选课成功");
            case -1:
                return ResponseObject.ofFail(999, "选课已满，无法继续选课");
            default:
                return ResponseObject.ofFail(999, "选课失败，请重试");
        }
    }

    @ApiOperation("学生退选教学班")
    @PostMapping(value = "/cancelTeachingClass")
    public ResponseObject<Object> cancelCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                               @ApiParam(value = "参数") @RequestBody(required = false) SelectCourseInput param) {
        Integer flag = studentService.cancelCourse(taskId, param);
        switch (flag) {
            case 1:
                return ResponseObject.ofSuccess("退选成功");
            default:
                return ResponseObject.ofFail(999, "退选失败，请重试");
        }
    }

    //***************************************************************************************
//    @ApiOperation("学生选课")
//    @PostMapping(value = "/selectCourse")
//    public ResponseObject<Object> selectCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
//                                               @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId,
//                                               @ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId,
//                                               @ApiParam(value = "退选教学班id", required = false) @RequestParam(required = false) Long cancelTeachingClassId) {
//        int res = studentService.selectCourse1(taskId, studentId, teachingClassId, cancelTeachingClassId);
//        switch (res) {
//            case 1:
//                return ResponseObject.ofSuccess("选课成功");
//            case -1:
//                return ResponseObject.ofFail(2, "选课已满，无法继续选课");
//            case -2:
//                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
//            case -3:
//                return ResponseObject.ofFail(5, "已添加相同课程下的教学班，请勿重复选择");
//            case -4:
//                return ResponseObject.ofFail(2, "不存在该教学班");
//            default:
//                return ResponseObject.ofFail(2, "选课失败，请重试");
//        }
//    }

    @ApiOperation("学生选课2")
    @PostMapping(value = "/selectCourseOptimize")
    public ResponseObject<Object> selectCourseOptimize(@ApiParam(value = "选课实体类", required = true) @RequestBody AddSelectCourse addSelectCourse) {
        int res = studentService.selectCourseOptimize(addSelectCourse);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("选课成功");
            case -1:
                return ResponseObject.ofFail(2, "选课已满，无法继续选课");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -3:
                return ResponseObject.ofFail(5, "已添加相同课程下的教学班，请勿重复选择");
            case -4:
                return ResponseObject.ofFail(2, "不存在该教学班");
            default:
                return ResponseObject.ofFail(2, "系统处理异常，选课失败");
        }
    }

    @ApiOperation("学生选课/退选-预选课")
    @PostMapping(value = "/selectPreCourse")
    public ResponseObject<Object> selectCoursePre(@ApiParam(value = "选课实体类", required = true) @RequestBody AddSelectCoursePre addSelectCoursePre) {
        int res = studentService.selectCoursePre(addSelectCoursePre);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("选课成功");
            case -1:
                return ResponseObject.ofFail(2, "选课已满，无法继续选课");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -3:
                return ResponseObject.ofFail(5, "已添加相同课程下的教学班，请勿重复选择");
            case -4:
                return ResponseObject.ofFail(2, "不存在该课程");
            case 2:
                return ResponseObject.ofSuccess("退选成功");
            default:
                return ResponseObject.ofFail(2, "系统处理异常，选课失败");
        }
    }

//    @ApiOperation("退选")
//    @PostMapping(value = "/cancelCourse")
//    public ResponseObject<String> cancelCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
//                                               @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId,
//                                               @ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId,
//                                               @ApiParam(value = "2退选   3管理员移除", required = true) @RequestParam Integer flag) {
//        int res = studentService.cancelCourse1(taskId, studentId, teachingClassId, flag);
//        switch (res) {
//            case 1:
//                return ResponseObject.ofSuccess("退选成功");
//            case -2:
//                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
//            case -4:
//                return ResponseObject.ofFail(2, "不存在该教学班");
//            default:
//                return ResponseObject.ofFail(2, "退选失败，请重试");
//        }
//    }

    @ApiOperation("退选2")
    @PostMapping(value = "/cancelCourseOptimize")
    public ResponseObject<String> cancelCourseOptimize(@ApiParam(value = "退选实体类", required = true) @RequestBody AddCancelCourse addCancelCourse) {
        int res = studentService.cancelCourseOptimize(addCancelCourse);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("退选成功");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -4:
                return ResponseObject.ofFail(2, "不存在该教学班");
            default:
                return ResponseObject.ofFail(2, "系统处理异常，退选失败");
        }
    }

    //***************************************************************************************
//    @ApiOperation("学生选择预选课")
//    @GetMapping(value = "/selectPr1eCourse")
//    public ResponseObject<String> selectPre1Course(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
//                                                  @ApiParam(value = "课程id ") @RequestParam(required = false) Long courseId,
//                                                  @ApiParam(value = "1、选择 2、放弃 ") @RequestParam(required = false) Integer flag,
//                                                  @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId) {
//        return ResponseObject.ofSuccess(studentService.selectPre1Course(taskId, courseId, flag, studentId));
//    }


    @ApiOperation("添加对应的学生")
    @GetMapping(value = "/addStudent")
    public ResponseObject<String> addStudent(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                             @ApiParam(value = "教学班id ") @RequestParam(required = false) Long teachingClassId,
                                             @ApiParam(value = "课程id ") @RequestParam(required = false) Long courseId,
                                             @ApiParam(value = "学生id ") @RequestParam(required = true) List<Long> studentIds) {

        TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
        for (Long studentId : studentIds) {
            teachingClassStudent.setTeachingClassId(teachingClassId);
            teachingClassStudent.setStudentId(studentId);
            teachingClassStudent.setCourseId(courseId);
            teachingClassStudent.setFlag(1);
            teachingClassStudentService.save(teachingClassStudent);
        }
        return ResponseObject.ofSuccess("学生添加成功");
    }

    @ApiOperation("判断是否已选择该课程")
    @GetMapping(value = "/isExistSelectCourse")
    public ResponseObject<TeachingClass> isExistSelectCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                             @ApiParam(value = "课程id ") @RequestParam(required = false) String courseId,
                                                             @ApiParam(value = "学生id ") @RequestParam(required = false) String studentId,
                                                             @ApiParam(value = "1、选择 2、放弃 ") @RequestParam(required = false) String studyNature,
                                                             @ApiParam(value = "1、选择 2、放弃 ") @RequestParam(required = false) String teachingClassType) {
        return ResponseObject.ofSuccess(studentService.isExistSelectCourse(taskId, courseId, studentId, studyNature, teachingClassType));
    }

    @ApiOperation("判断学生是否是禁选学生")
    @GetMapping(value = "/isProhibitStudent")
    public ResponseObject<List<CssProhibitStudent>> isProhibitStudent(@ApiParam(value = "学生id ") @RequestParam(required = false) Long studentId) {
        return ResponseObject.ofSuccess(studentService.isProhibitStudent(studentId));
    }

    @ApiOperation("获取学生选课数据")
    @PostMapping(value = "/getStudentTeachingClassView")
    public ResponseObject<StudentTeachingClassView> getAvailableClass(@ApiParam(value = "学生筛选数据入参", required = true) @RequestBody GetAvailableCondition getAvailableCondition) {
        return ResponseObject.ofSuccess(studentService.getAvailableTeachingClass(getAvailableCondition));
    }

}
